<?php
class Zendvn_Plugin_Permission extends Zend_Controller_Plugin_Abstract{
	
	public function preDispatch(Zend_Controller_Request_Abstract $request){
		
		$auth = Zend_Auth::getInstance();
		//Lay ten cua module,controller,action
		$moduleName = $this->_request->getModuleName();
		$controllerName = $this->_request->getControllerName();
		$actionName = $this->_request->getActionName();
		
		//----------START-KIEM TRA QUYEN TRUY CAP VAO ADMIN -------------
		

		//Neu controllerName == admin | admin-group | admin-xxxxxx thi gan $flagAdmin=true
		$flagAdmin = false;
		if($controllerName == 'admin'){
			$flagAdmin = true;
		}else{
			$tmp = explode('-',$controllerName);
			if($tmp[0] == 'admin'){
				$flagAdmin = true;
			}
		}
		
		//set quyen truy cap khi duoc dang nhap la group_acp = 1
		$flagPage = 'none';
		if($flagAdmin == true){
			if($auth->hasIdentity() == false){
				$flagPage = 'login';
			}else{
				$info = new Zendvn_System_Info();
				$group_acp = $info->getGroupInfo('group_acp');
				if($group_acp != 1){
					$flagPage = 'no-access';
				}else{
					$aclInfo = $info->getAclInfo();
					$acl = new Zendvn_System_Acl($aclInfo);
					$arrParams = $this->_request->getParams();
					if($acl->isAllowed($arrParams) == false){
						$flagPage = 'no-access-acl';
					}
					
				}
			}
		}
	//----------END-KIEM TRA QUYEN TRUY CAP VAO ADMIN -------------
	//echo '<br>' . $flagPage;
		if($flagPage != 'none'){
			if($flagPage == 'login'){
				$this->_request->setModuleName('default');
				$this->_request->setControllerName('public');
				$this->_request->setActionName('login');
			}
				
			if($flagPage == 'no-access'){
				$this->_request->setModuleName('default');
				$this->_request->setControllerName('public');
				$this->_request->setActionName('no-access');
			}
			if($flagPage == 'no-access-acl'){
				$this->_request->setModuleName('default');
				$this->_request->setControllerName('public');
				$this->_request->setActionName('no-access-acl');
			}
		}
		

	}
}